package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.callshow.CallShowEntry;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityWallpaperBinding;
import flc.ast.databinding.ActivityWallpaperDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class WallpaperDetailActivity extends BaseAc<ActivityWallpaperDetailBinding> {

    public static StkResBean wallpaperBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_wallpaper_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWallpaperDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);
        mDataBinding.ivSet.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSave://保存
                showDialog(getString(R.string.save_ing));
                Downloader.newTask(mContext)
                        .url(wallpaperBean.getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                dismissDialog();
                                ToastUtils.showShort(getString(R.string.save_to_album));
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                dismissDialog();
                                ToastUtils.showShort(R.string.save_failure);
                            }
                        });
                break;
            case R.id.ivCollect://收藏
                List<StkResBean> listSp = AppSpUtils.getWallpaperCollectList();
                if (wallpaperBean.isSelected()) {
                    wallpaperBean.setSelected(false);
                    listSp.remove(wallpaperBean);
                    AppSpUtils.savWallpaperCollectList(listSp);
                } else {
                    if (listSp != null && listSp.size() > 0) {
                        listSp.add(wallpaperBean);
                        AppSpUtils.savWallpaperCollectList(listSp);
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        listSave.add(wallpaperBean);
                        AppSpUtils.savWallpaperCollectList(listSave);
                    }
                    wallpaperBean.setSelected(true);
                }
                isCollect();
                break;
            case R.id.ivSet://设为壁纸
                Downloader.newTask(mContext)
                        .url(wallpaperBean.getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                CallShowEntry.setWallpaper(uri, new CallShowEntry.SetWallpaperCallback() {
                                    @Override
                                    public void onStart() {
                                        showDialog(getString(R.string.setting_wallpaper));
                                    }

                                    @Override
                                    public void onSetResult(boolean success) {
                                        dismissDialog();
                                        if (success) {
                                            ToastUtils.showShort(getString(R.string.setting_suc));
                                        } else {
                                            ToastUtils.showShort(R.string.setting_failure);
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                ToastUtils.showShort(R.string.setting_failure);
                            }
                        });
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(wallpaperBean.getUrl()).into(mDataBinding.ivWallpaperImg);
        isCollect();

    }

    private void isCollect() {
        if (wallpaperBean.isSelected()) {
            mDataBinding.ivCollect.setImageResource(R.drawable.icon_sc2b);
        } else {
            mDataBinding.ivCollect.setImageResource(R.drawable.icon_sc2a);
        }
    }

}